//
//  Units.m
//  iBoxPay
//
//  Created by Sven on 11-8-15.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "Units.h"



/******************************************************************************************/
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation UINavigationItem(TitleColorAddition)

- (void)setTitleColor:(UIColor *)color
{
    UILabel* titleLabel = (UILabel *)(self.titleView);
    if (!titleLabel) {
        titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(100, 2, 120, 40)] autorelease];
        titleLabel.adjustsFontSizeToFitWidth = YES;
        titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        titleLabel.textAlignment = UITextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize:18];
        titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.shadowColor = [UIColor whiteColor];
		titleLabel.shadowOffset = CGSizeMake(0, -0.5);
        self.titleView = titleLabel;
    }
    titleLabel.text = self.title;
    titleLabel.textColor = color;
}

@end



@implementation UINavigationController (ViewControllers)

- (UIViewController *)viewControllerForClass:(Class)c
{
	for (UIViewController* v in self.viewControllers) {
		if ([v isKindOfClass:c] && [v isMemberOfClass:c]) {
			return v;
		}
	}
	
	return nil;
}

@end


@implementation UINavigationBar (UINavigationBarBackgroundImage)

-(void)setBackgroundImage:(UIImage*)image
{
	UIImageView* backgroundIV = (UIImageView *)[self viewWithTag:0x1024];
    if(image == nil){
		if (backgroundIV) {
			[backgroundIV removeFromSuperview];
		}
	}
	else {
		if (!backgroundIV) {
			CGRect frame = CGRectMake(0.f, 0.f, self.frame.size.width, self.frame.size.height);
			backgroundIV = [[[UIImageView alloc] initWithFrame:frame] autorelease];
			backgroundIV.autoresizingMask  = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
			backgroundIV.tag = 0x1024;
		}
		
		backgroundIV.image = image;
		[self insertSubview:backgroundIV atIndex:0];
		[self sendSubviewToBack:backgroundIV];
	}	
}


//for other views
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
{
    [super insertSubview:view atIndex:index];
	
	UIImageView* backgroundIV = (UIImageView *)[self viewWithTag:0x1024];
	if (backgroundIV) {
		[self sendSubviewToBack:backgroundIV];
	}
}

@end
